<?php

namespace App\Http\Requests\School;

use App\Models\UserRole;
use Illuminate\Foundation\Http\FormRequest;

/**
 * Class StoreImportUser
 * @package App\Http\Requests\School
 */
class StoreImportUser extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'role' => [
                'required',
                'integer',
                function ($attribute, $value, $fail) {
                    if (UserRole::query()->where('school_id', \Auth::user()->school_id)->find($value)->doesntExist()) {
                        return $fail('该用户角色不存在');
                    }
                }
            ],
            'users' => 'required|array'
        ];
    }

    /**
     * @return array
     */
    public function attributes()
    {
        return [
            'role' => '用户角色',
            'users' => 'Xlsx表格'
        ];
    }

    /**
     * @return array
     */
    public function messages()
    {
        return [
            'role.required' => '请选择:attribute',
            'role.integer' => ':attribute数据类型错误',
            'users.required' => '请导入:attribute',
            'users.array' => ':attribute数据类型错误'
        ];
    }
}
